package com.rkc.controller;


import com.rkc.component.FileService;
import com.rkc.enums.BizCodeEnum;
import com.rkc.request.UserLoginRequest;
import com.rkc.request.UserRegisterRequest;
import com.rkc.service.UserService;
import com.rkc.utils.JsonData;
import com.rkc.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 冉堃赤
 * @since 2021-10-07
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FileService fileService;

    /**
     * 上传头像，默认最大1M，超过报错
     * @param file 文件
     * @return
     */
    @PostMapping("upload")
    @ApiOperation("用户头像上传")
    public JsonData uploadUserImage(@ApiParam(value = "文件上传", required = true) @RequestPart("file") MultipartFile file) {
        String url = fileService.upload(file);
        return url == null ? JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL) : JsonData.buildSuccess(url);
    }

    @PostMapping("register")
    @ApiOperation("用户注册")
    public JsonData register(@ApiParam("用户注册对象") @RequestBody UserRegisterRequest registerRequest) {
        return userService.register(registerRequest);
    }

    @PostMapping("login")
    @ApiOperation("用户登录")
    public JsonData login(@ApiParam("用户登录对象") @RequestBody UserLoginRequest loginRequest) {
        return userService.login(loginRequest);
    }

    @ApiOperation("个人信息查询")
    @GetMapping("detail")
    public JsonData detail() {
        UserVO userVO = userService.findUserDetail();
        return JsonData.buildSuccess(userVO);
    }
}

